package edu.scau.hibernate.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.*;

import edu.scau.hibernate.service.UserService;
import edu.scau.hibernate.service.*;

public class LoginDialog extends JFrame{
	private JLabel jlbuser=new JLabel("         用户名：");
	private JTextField jtfuser=new JTextField(18);
	private JLabel jlbpassword=new JLabel("        密    码：");
	private JPasswordField jtfpassword=new JPasswordField(18);
	private JButton jbtLogin=new JButton("登录");
	//private JButton jbtRegist=new JButton("注册");
	private JButton jbtCancel=new JButton("取消");
	UserService serviceImpl =null;
	
	public LoginDialog(){

		jtfpassword.setEchoChar('*');
		//界面排版设计
		JPanel p=new JPanel(null);
		JLabel b1=new JLabel(new ImageIcon("login.png"));
		JLabel b2=new JLabel(new ImageIcon("user.png"));
		JLabel b3=new JLabel(new ImageIcon("lock.png"));
		
		b1.setBounds(0, 0, 420, 60);
		b2.setBounds(98, 60, 20,20);
		jlbuser.setBounds(100,60,100,20);
		jtfuser.setBounds(180, 60, 120, 20);
		b3.setBounds(98, 80, 20, 20);
		jlbpassword.setBounds(100,80,100,20);
		jtfpassword.setBounds(180,80,120,20);
		jbtLogin.setBounds(150,150,60,20);
		//jbtRegist.setBounds(190, 150, 60, 20);
		jbtCancel.setBounds(230, 150, 60, 20);
		
		p.add(b1);
		p.add(b2);
		p.add(b3);
		p.add(new JLabel(new ImageIcon("login.png")));
		p.add(jlbuser);
		p.add(jtfuser);
		p.add(jlbpassword);
		p.add(jtfpassword);
		p.add(jbtLogin);
		//p.add(jbtRegist);
		p.add(jbtCancel);
		this.add(p);
		//为按钮添加监听器
		
		//jbtLogin.addActionListener(this);
		//jbtRegist.addActionListener(this);
		//jbtCancel.addActionListener(this);
		
	
//		为文本框添加鼠标监听器
		jtfuser.addMouseListener(new MouseAdapter(){
			public void mousePressed(MouseEvent e){//文本框中按下鼠标，将内容全部清除
				jtfuser.setText("");
			}
		});
		

		/*register.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				RegisterDialog rd=new RegisterDialog();
				rd.setVisible(true);
				rd.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
			}
		});
		*/
		jbtCancel.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
				
			}
			
		});
		jbtLogin.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				MainFrame mainFrame;
				UserService service;
				try {
					service=new UserServiceImpl();
					if(service.loginCheck(jtfuser.getText().trim(), String.valueOf(jtfpassword.getPassword()))){
						mainFrame = new MainFrame();
						mainFrame.setVisible(true);
						mainFrame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
						setVisible(false);
					}
					else
						JOptionPane.showMessageDialog(null, "用户名或密码错误！");
				} catch (Exception e1) {
					// TODO Auto-generated catch block

					e1.printStackTrace();
				}
			}
		});
	}
	

	public static void main(String[] args) {
		LoginDialog login=new LoginDialog();
		login.setTitle("用户管理系统");
		login.setSize(420, 260);
		login.setLocationRelativeTo(null);
		login.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		login.setVisible(true);
		login.setResizable(false);

	}

	
}